import cn.hutool.core.io.file.FileReader;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;

/**
 * @author wjj
 */
public class JSONSchemaTest {

    private static final ObjectMapper om = new ObjectMapper();

    private static final String PRODUCT_TYPE_JSON;

    private static final String PRODUCT_TYPE_JSON_SCHEMA;

    private static final String PROPERTY_GROUP_PATH_PREFIX = "$.propertyGroups.{0}.";

    private static final Map<String, Object> FIELD_MAP;

    static {
        FileReader productTypeJsonReader = new FileReader("E:\\yanjiu_json_schema\\product_type_hardware_clamp_vise.json");
        PRODUCT_TYPE_JSON = productTypeJsonReader.readString();

        FileReader productTypeSchemaJsonReader = new FileReader("E:\\yanjiu_json_schema\\hardware_clamp_vise.json");
        String tmpStr = productTypeSchemaJsonReader.readString();

        Map<String, Object> defsMap = JsonPath.read(tmpStr,"$.$defs");
        String defs;
        JsonRefReplacer refReplacer;
        try {
            defs = om.writeValueAsString(defsMap);
            // 处理$refs
            refReplacer = new JsonRefReplacer(JSONUtil.toJsonStr(defs));
            PRODUCT_TYPE_JSON_SCHEMA = refReplacer.replaceProperties(tmpStr);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        FIELD_MAP = new HashMap<>(100);
    }

    public void productTypeConvert() throws JsonProcessingException {
        // 1. 根据ProductType Definitions API获取到的 Product Type属性根据offer,images等进行分类, 共7类. 可由前端页面自有组合
        JsonNode rootNode = om.readTree(PRODUCT_TYPE_JSON);
        JsonNode propertyGroupsNode = rootNode.get("propertyGroups");
        Iterator<String> keys = propertyGroupsNode.fieldNames();
        while (keys.hasNext()) {
            String key = keys.next();

            String title = JsonPath.read(PRODUCT_TYPE_JSON, MessageFormat.format(PROPERTY_GROUP_PATH_PREFIX + "title", key));
            String description = JsonPath.read(PRODUCT_TYPE_JSON, MessageFormat.format(PROPERTY_GROUP_PATH_PREFIX + "description", key));
            List<String> propertyNames = JsonPath.read(PRODUCT_TYPE_JSON, MessageFormat.format(PROPERTY_GROUP_PATH_PREFIX + "propertyNames", key));

            Map<String, Map<String, Object>> propertyDetails = new HashMap<>();
            propertyNames.forEach(propertyName -> {
                Map<String, Object> detail = findPropertyDetailFromSchemaProperties(propertyName);
                propertyDetails.put(propertyName, detail);
            });

            Map<String, Object> detailMap = new HashMap<>(3);
            detailMap.put("title", title);
            detailMap.put("description", description);
            detailMap.put("properties", propertyDetails);

            FIELD_MAP.put(key, detailMap);
        }
        System.out.println(om.writerWithDefaultPrettyPrinter().writeValueAsString(FIELD_MAP));
    }

    public Map<String, Object> findPropertyDetailFromSchemaProperties(String propertyName) {
        String path = "$.properties." + propertyName;
        return JsonPath.read(PRODUCT_TYPE_JSON_SCHEMA, path);
    }


    public static void main(String[] args) throws JsonProcessingException {
        JSONSchemaTest test = new JSONSchemaTest();
        test.productTypeConvert();
    }
}
